<?php

namespace App\Common;


use App\Constants\ConstCode;
use App\Model\Setting;
use Hyperf\Config\Annotation\Value;

trait StringUtils
{
    /**
     * 登陆验证
     * @param $number
     * @param $password
     * @return string
     */
    function basic_token($number, $password)
    {
//        return base64_encode($number . ":" . $password);
        return md5($number . ":" . $password);
    }

    public function getSettingContent($code)
    {
        $setting = Setting::where(['code' => $code])->first();
        if (empty($setting)) {
            return '';
        } else {
            $content = json_decode($setting['content'], true);
            foreach ($content as $value) {
                if ($value['field'] == $code) {
                    return $value['content'];
                }
            }
            return '';
        }
    }

    /**
     * @param $loginStatus
     * @return string
     */
    public function getDeviceSql($loginStatus):string
    {
       $sqlRew = "SELECT
        id,
        market_user_type,
        AVATAR_URL,
        UNION_ID,
        EMAIL,
        JOB,
        MOBILE,
        SEX,
        STATUS,
        ORG_ID,
        NICK_NAME,
        tableStatus ,
        projectName,
        projectId,
        userTableId,
        seat_name,
        loginStatus,
        any_value(VIRTUAL_NAME) VIRTUAL_NAME,
        ifNULL(friendsNumber,0) as friendsNumber,
        CORP_FULL_NAME,
        CORP_ID,
        COPR_SHORT_NAME,
        any_value(login_time) login_time
        FROM
        (
        SELECT
        t.id,
        t.market_user_type,
        AVATAR_URL,
        UNION_ID,
        CORP_ID,
        t6.COPR_SHORT_NAME,
        t6.CORP_FULL_NAME,
        t.EMAIL,
        JOB,
        MOBILE,
        t.SEX,
        t.STATUS,
        t7.dept_id as ORG_ID,
        t7.dept_name as COMPANY_NAME,
        concat ( IFNULL(t.REAL_NAME,''),'/', IFNULL(t.NAME,''),'/', IFNULL(t.ACCT_ID,'') ) AS NICK_NAME,
        IF
        ( ISNULL( t1.seat_id ), 0, 1 ) AS tableStatus,
        p.NAME AS projectName,
        p.id AS projectId,
        t2.user_id AS userTableId,
        t2.NAME AS seat_name,
        IF
        ( ISNULL( t3.id ), 1, 0 ) AS loginStatus,
        CONCAT( IFNULL(t4.ip,''), '_', IFNULL(t3.SEQ,'') ) AS VIRTUAL_NAME,
        t5.friendsNumber,
        t9.login_time
        FROM
        market_account t
        LEFT JOIN corporation_info t6 ON t6.id = t.corp_id
        LEFT JOIN vm_info t3 ON t3.user_id = t.id
        LEFT JOIN pc_instance t4 ON t3.pc_instance_id = t4.id
        LEFT JOIN market_account_seats t1 ON t.id = t1.MARKET_USER_ID
        LEFT JOIN sys_user t2 ON t1.seat_id = t2.user_id
        LEFT JOIN sys_dept t7 on t7.dept_id = t2.dept_id
        LEFT JOIN PROJECT_info P ON P.id = t1.PROJECT_ID
        LEFT JOIN ( SELECT MARKET_USER_ID, count( 1 ) AS friendsNumber FROM account_external_contacts GROUP BY
        MARKET_USER_ID ) t5 ON t.id = t5.MARKET_USER_ID
        LEFT JOIN pc_ins_login_his t9 on t9.market_user_id = t.id
        )A where loginStatus = {$loginStatus}  -- 1:离线； 0:在线
        group by id
        order by login_time desc limit 2";
       return $sqlRew;
    }

    /**
     * @param $loginStatus
     * @return string
     */
    public function getDeviceSqlRew($loginStatus):string
    {
        $sqlRew = "SELECT
        id,
        market_user_type,
        AVATAR_URL,
        UNION_ID,
        EMAIL,
        JOB,
        MOBILE,
        SEX,
        STATUS,
        ORG_ID,
        NICK_NAME,
        tableStatus ,
        projectName,
        projectId,
        userTableId,
        seat_name,
        loginStatus,
        any_value(VIRTUAL_NAME) VIRTUAL_NAME,
        ifNULL(friendsNumber,0) as friendsNumber,
        CORP_FULL_NAME,
        CORP_ID,
        COPR_SHORT_NAME,
        any_value(login_time) login_time
        FROM
        (
        SELECT
        t.id,
        t.market_user_type,
        AVATAR_URL,
        UNION_ID,
        CORP_ID,
        t6.COPR_SHORT_NAME,
        t6.CORP_FULL_NAME,
        t.EMAIL,
        JOB,
        MOBILE,
        t.SEX,
        t.STATUS,
        t7.dept_id as ORG_ID,
        t7.dept_name as COMPANY_NAME,
        concat ( IFNULL(t.REAL_NAME,''),'/', IFNULL(t.NAME,''),'/', IFNULL(t.ACCT_ID,'') ) AS NICK_NAME,
        IF
        ( ISNULL( t1.seat_id ), 0, 1 ) AS tableStatus,
        p.NAME AS projectName,
        p.id AS projectId,
        t2.user_id AS userTableId,
        t2.NAME AS seat_name,
        IF
        ( ISNULL( t3.id ), 1, 0 ) AS loginStatus,
        CONCAT( IFNULL(t4.ip,''), '_', IFNULL(t3.SEQ,'') ) AS VIRTUAL_NAME,
        t5.friendsNumber,
        t9.login_time
        FROM
        market_account t
        LEFT JOIN corporation_info t6 ON t6.id = t.corp_id
        LEFT JOIN vm_info t3 ON t3.user_id = t.id
        LEFT JOIN pc_instance t4 ON t3.pc_instance_id = t4.id
        LEFT JOIN market_account_seats t1 ON t.id = t1.MARKET_USER_ID
        LEFT JOIN sys_user t2 ON t1.seat_id = t2.user_id
        LEFT JOIN sys_dept t7 on t7.dept_id = t2.dept_id
        LEFT JOIN PROJECT_info P ON P.id = t1.PROJECT_ID
        LEFT JOIN ( SELECT MARKET_USER_ID, count( 1 ) AS friendsNumber FROM account_external_contacts GROUP BY
        MARKET_USER_ID ) t5 ON t.id = t5.MARKET_USER_ID
        LEFT JOIN pc_ins_login_his t9 on t9.market_user_id = t.id
        )A where loginStatus = {$loginStatus}  -- 1:离线； 0:在线
        group by id
        order by login_time desc";
        return $sqlRew;
    }



}